#ifndef FLIP_DRAW_H
#define FLIP_DRAW_H

#include "../gvl/support/platform.hpp"

#define FLIP_VERTEX_BUFFER_SIZE (1024)

typedef struct flip_draw_buffer_
{
	int mode;
	int textured;
	int colored;
	float vertices[FLIP_VERTEX_BUFFER_SIZE*2];
	float texcoords[FLIP_VERTEX_BUFFER_SIZE*2];
	float colors[FLIP_VERTEX_BUFFER_SIZE*3];
	float* cur_vert;
	float* cur_tex;
	float* cur_col;
	
	float colr, colg, colb;
	
} flip_draw_buffer;

void flip_draw_init(flip_draw_buffer* self);
void flip_draw_clear(flip_draw_buffer* self);
void flip_draw_color(flip_draw_buffer* self, float r, float g, float b);

#define PTR_DIFF(x, y) (((char const*)(x))-((char const*)(y)))

void flip_draw_flush(flip_draw_buffer* self);

GVL_INLINE void flip_draw_vertex_color(flip_draw_buffer* self, int count)
{
	if(self->colored)
	{
		int i;
		for(i = 0; i < count; ++i)
		{
			*self->cur_col++ = self->colr;
			*self->cur_col++ = self->colg;
			*self->cur_col++ = self->colb;
		}
	}
}


GVL_INLINE void flip_draw_set_texture(flip_draw_buffer* self, int enabled)
{
	if(self->textured != enabled)
	{
		flip_draw_flush(self);
		self->textured = enabled;
	}
}

void flip_draw_fill_colors(flip_draw_buffer* self);

GVL_INLINE void flip_draw_set_color(flip_draw_buffer* self, int enabled)
{
	if(self->colored != enabled)
	{
		flip_draw_fill_colors(self);
		self->colored = enabled;
	}
}

GVL_INLINE void flip_draw_quad(flip_draw_buffer* self,
	float x1, float y1, float x2, float y2,
	float x3, float y3, float x4, float y4)
{
	if(PTR_DIFF(&self->vertices[FLIP_VERTEX_BUFFER_SIZE], self->cur_vert) < 4*2*sizeof(float))
		flip_draw_flush(self);
	flip_draw_set_texture(self, 0);
	flip_draw_vertex_color(self, 4);
	*self->cur_vert++ = x1;
	*self->cur_vert++ = y1;
	*self->cur_vert++ = x2;
	*self->cur_vert++ = y2;
	*self->cur_vert++ = x3;
	*self->cur_vert++ = y3;
	*self->cur_vert++ = x4;
	*self->cur_vert++ = y4;
}

#endif // FLIP_DRAW_H
